package com.ice.base.controller;

import com.ice.base.constant.BaseConstant;
import com.ice.base.model.SysLoginModel;
import com.ice.base.model.SysLoginRoleModel;
import com.ice.base.model.SysRoleModel;
import com.ice.base.service.SysLoginRoleService;
import com.ice.base.service.SysLoginService;
import com.ice.base.service.SysRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@RestController
@ApiIgnore(value ="系统后台-角色管理")
public class SysRoleController extends BaseController {

    //---------------------------角色-----------------------start
    @Resource
    private SysRoleService sysRoleService;
    @Resource
    private SysLoginService sysLoginService;
    @Resource
    private SysLoginRoleService sysLoginRoleService;



    @ApiOperation(value = "角色-插入")
    @PostMapping("/base/SysRole/insert")
    public HashMap insert_SysRole(HttpServletRequest request,SysRoleModel model) {

        return insert(request, sysRoleService,model);
    }

    @ApiOperation(value = "角色-删除")
    @PostMapping("/base/SysRole/deleteById")
    public HashMap deleteById_SysRole(@RequestParam("id") String id) {
        //还应该删除角色权限中间表
        return delete(sysRoleService, id);
    }

    @ApiOperation(value = "角色-更新")
    @PostMapping("/base/SysRole/updateById")
    public HashMap update_SysRole(HttpServletRequest request,SysRoleModel model) {
        return update(request, sysRoleService,model);
    }

    @ApiOperation(value = "角色-查询一个")
    @PostMapping("/base/SysRole/selectById")
    public HashMap selectById_SysRole(@RequestParam("id") String id, @RequestParam(name = "selectDetails", required = false) boolean selectDetails) {
        if (selectDetails) {
            HashMap<String, Object> resultMap = getReturnMap(BaseConstant.Response_MENU.REQUEST_SELECT_FAILED);
            //查询一个角色
            SysRoleModel sysRoleModel = sysRoleService.selectDetailsById(id);
            if (sysRoleModel != null) {
                getReturnMapSelect(resultMap, sysRoleModel);
            }
            return resultMap;
        } else {
            return selectById(sysRoleService, id);
        }
    }


    @ApiOperation(value = "角色-查询多个")
    @PostMapping("/base/SysRole/selectList")
    public HashMap selectList_SysRole(HttpServletRequest request, @RequestParam(name = "selectDetails", required = false) boolean selectDetails) {
        SysRoleModel baseModel = new SysRoleModel();
        if (selectDetails) {
            HashMap<String, Object> resultMap = getReturnMap(BaseConstant.Response_MENU.REQUEST_SELECT_FAILED);
            baseModel.getInstanceForSelect(request, baseModel);
            List<SysRoleModel> sysRoleModels = sysRoleService.selectListDetails(baseModel);
            getReturnMapSelect(resultMap, sysRoleModels);
            return resultMap;
        } else {
            return selectListByCondition(request, sysRoleService, baseModel);
        }
    }

    @ApiOperation(value = "角色-查询分页")
    @PostMapping("/base/SysRole/selectListByPageHelper")
    public HashMap selectListByPageHelper_SysRole(HttpServletRequest request,
                                                  @RequestParam(name = "pageSize") Integer pageSize,
                                                  @RequestParam(name = "pageNum") Integer pageNum) {
        SysRoleModel model = new SysRoleModel();
        return selectListByPageHelper(request, sysRoleService,model);
    }
    //---------------------------角色-----------------------end


    @ApiOperation(value = "角色-根据用户id查询用户的角色")
    @PostMapping("/base/RBAC/selectByLoginId")
    public HashMap selectByLoginId_RBAC(@RequestParam("id") Integer id) {
        HashMap<String, Object> resultMap = getReturnMap(BaseConstant.Response_MENU.REQUEST_SELECT_FAILED);
        //查询一个角色
        SysLoginModel sysRoleModel = sysLoginService.selectDetailsById(id);
        ArrayList<Object> resultDataList = new ArrayList<>();
        if (sysRoleModel != null) {
            List<SysRoleModel> roles = sysRoleModel.getRoles();
            if (roles != null) {
                for (SysRoleModel roleModel : roles) {
                    HashMap<String, Object> stringObjectHashMap = new HashMap<>();
                    stringObjectHashMap.put("id", roleModel.getId());
                    stringObjectHashMap.put("name", roleModel.getRole_name());
                    stringObjectHashMap.put("isParent", true);
                    resultDataList.add(stringObjectHashMap);
                }
                resultMap.put("data", resultDataList);
                getReturnMapSuccess(resultMap);
            }else{
                resultMap.put("message","用户没有角色权限！");
            }
        }
        return resultMap;

    }


    @ApiOperation(value = "RBAC-插入(给账号授权角色)")
    @PostMapping("/base/RBAC/insert")
    public HashMap insert_RBACLoginRole(@RequestParam("login_id") Integer login_id, @RequestParam("role_id") String role_id) {
        SysLoginRoleModel sysLoginRoleModel = new SysLoginRoleModel();
        sysLoginRoleModel.init(sysLoginRoleModel);
        sysLoginRoleModel.setLogin_id(login_id);
        sysLoginRoleModel.setRole_id(role_id);
        return insert(sysLoginRoleService, sysLoginRoleModel);
    }


    @ApiOperation(value = "角色-删除用户的角色")
    @PostMapping("/base/RBAC/deleteById")
    public HashMap deleteById_RBACLoginRole(@RequestParam("login_id") Integer login_id, @RequestParam("role_id") String role_id) {
        return getHashMap(sysLoginRoleService.deleteByLoginIdAndRoleId(login_id, role_id));
    }



    private HashMap getHashMap(Integer integer2) {
        HashMap<String, Object> returnMap = getReturnMap();
        if (integer2 == 1) {
            getReturnMapSuccess(returnMap);
        }
        return returnMap;
    }
}
